<?php
/*
 * Copyright (c) 2009 Tom Smith (tom@takeontom.com).
 * 
 * Distributed under the terms of the GNU Lesser General Public License.
 * 
 * ---------------------------------------------------------------------------------
 * 
 * This file is part of Seraph PHP Framework.
 *
 * Seraph PHP Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Seraph PHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seraph PHP Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once (SRC_DIR . '/framework/Model/Model.php');

require_once (TEST_DIR . '/_simpletest/mock_objects.php');

require_once (SRC_DIR . '/framework/DB/DBConnection.php');

Mock::generate('DBConnection');

class TestModel extends UnitTestCase	{
	
	public function setup()	{
		
	}
	
	public function testAddingEntityNames()	{
		Model::clearEntityNames();
		$this->assertEqual(Model::getEntityNames(), array());
		
		$testNames = array('test_name1', 'test_name2');
		Model::setEntityNames($testNames);
		$this->assertEqual(Model::getEntityNames(), $testNames);
		
		$this->assertFalse(Model::isAnEntityName('test_name3'));
		
		$testNames[] = 'test_name3';
		Model::addEntityName('test_name3');
		$this->assertTrue(Model::isAnEntityName('test_name3'));
		$this->assertEqual(Model::getEntityNames(), $testNames);
		
		
		Model::clearEntityNames();
		$this->assertEqual(Model::getEntityNames(), array());
	}
	
	public function testIsClassnameAnEntity()	{
		Model::clearEntityNames();
		Model::addEntityName('user');
		$this->assertTrue(Model::isClassNameAnEntity('user'));
		
		$this->assertFalse(Model::isClassNameAnEntity('something_random'));
		$this->assertFalse(Model::isClassNameAnEntity('HairyBum'));
		Model::clearEntityNames();
	}
	
	public function testLoadValidEntityNamesFromDatabaseTableNames()	{
		Model::clearEntityNames();
		
		$mockDB = new MockDBConnection();
		$mockDB->setReturnValue('getTableNames', array('wibble', 'wobble', 'nobble', 'nibble', 'nipple'));
		
		Model::addDatabase('test-db', $mockDB);
		
		$this->assertFalse(Model::isAnEntityName('wibble'));
		$this->assertFalse(Model::isAnEntityName('wobble'));
		$this->assertFalse(Model::isAnEntityName('nobble'));
		$this->assertFalse(Model::isAnEntityName('nibble'));
		$this->assertFalse(Model::isAnEntityName('nipple'));
		
		Model::generateValidEntityNamesFromDB();
		
		$this->assertTrue(Model::isAnEntityName('wibble'));
		$this->assertTrue(Model::isAnEntityName('wobble'));
		$this->assertTrue(Model::isAnEntityName('nobble'));
		$this->assertTrue(Model::isAnEntityName('nibble'));
		$this->assertTrue(Model::isAnEntityName('nipple'));
		
		Model::clearDatabaseConnections();
		Model::clearEntityNames();
		
		//test that the clearing worked
		$this->assertFalse(Model::isAnEntityName('wibble'));
		$this->assertFalse(Model::isAnEntityName('wobble'));
		$this->assertFalse(Model::isAnEntityName('nobble'));
		$this->assertFalse(Model::isAnEntityName('nibble'));
		$this->assertFalse(Model::isAnEntityName('nipple'));
		
	}
}